<?php
// +----------------------------------------------------------------------
// | likeadmin快速开发前后端分离管理后台（PHP版）
// +----------------------------------------------------------------------
// | 欢迎阅读学习系统程序代码，建议反馈是我们前进的动力
// | 开源版本可自由商用，可去除界面版权logo
// | gitee下载：https://gitee.com/likeshop_gitee/likeadmin
// | github下载：https://github.com/likeshop-github/likeadmin
// | 访问官网：https://www.likeadmin.cn
// | likeadmin团队 版权所有 拥有最终解释权
// +----------------------------------------------------------------------
// | author: likeadminTeam
// +----------------------------------------------------------------------

namespace app\adminapi\validate\reimbursement;


use app\common\validate\BaseValidate;
use app\common\model\user\User;


/**
 * ExpenseApplication验证器
 * Class UserExpenseApplicationValidate
 * @package app\adminapi\validate\reimbursement
 */
class ExpenseApplicationValidate extends BaseValidate
{

//    public function __construct()
//    {
//        // 确保User模型中有数据
//        $nicknames = User::column('nickname');
//        if (empty($nicknames)){
//            throw new \Exception("User model has no data or 'nickname' field is empty");
//        }
//        $this->rule['applicant_men'] = 'require|in:' . implode(',', $nicknames);
//    }

     /**
      * 设置校验规则
      * @var string[]
      */
    protected $rule = [
        'id' => 'require',
        'applicant_id' => 'require|number',
        'expense_type_id' => 'require|number',
        'amount' => 'float|gt:0',
        'description' => 'max:200',
        'approve_status' => 'require|in:2,3',
        'remark' => 'max:200'
    ];

    protected $message = [
        'id.require' => '记录ID不能为空',
        'expense_type_id.require' => '费用类型不能为空',
        'amount.require' => '申请金额不能为空',
        'amount.float' => '申请金额格式错误',
        'amount.gt' => '申请金额必须大于0',
        'description.max' => '描述不能超过200个字符',
        'approve_status.require' => '审批状态不能为空',
        'approve_status.in' => '审批状态值无效',
        'remark.max' => '审批备注不能超过200个字符'
    ];


    /**
     * 参数描述
     * @var string[]
     */
    protected $field = [
        'applicant_id' => '申请人',
        'expense_type_id' => '费用类型',
        'amount' => '申请金额',
        'current_status' => '申请状态',
        'description' => '描述',
        'approve_status' => '审批状态',
        'remark' => '审批备注'
    ];


    /**
     * @notes 添加场景
     * @return ExpenseApplicationValidate
     * @author likeadmin
     * @date 2025/05/16 11:36
     */
    public function sceneAdd()
    {
        return $this->only(['applicant_id','expense_type_id','amount', 'description'])
            ->appendRule([
                'applicant_id' => 'require|number',
                'expense_type_id' => 'require|number',
                'amount' => 'require|float'
            ]);
    }


    /**
     * @notes 编辑场景
     * @return ExpenseApplicationValidate
     * @author likeadmin
     * @date 2025/05/16 11:36
     */
    public function sceneEdit()
    {
        return $this->only(['id', 'expense_type_id','amount', 'description']);
    }


    /**
     * @notes 删除场景
     * @return ExpenseApplicationValidate
     * @author likeadmin
     * @date 2025/05/16 11:36
     */
    public function sceneDelete()
    {
        return $this->only(['id']);
    }


    /**
     * @notes 详情场景
     * @return ExpenseApplicationValidate
     * @author likeadmin
     * @date 2025/05/16 11:36
     */
    public function sceneDetail()
    {
        return $this->only(['id']);
    }

    /**
     * @notes 审批场景
     * @return ExpenseApplicationValidate
     */
    public function sceneApprove()
    {
        return $this->only(['id', 'approve_status', 'remark']);
    }

    // 自定义验证方法
    public function checkApplicantMen($value)
    {
        if (!User::where('nickname', $value)->find()) {
            return '申请人不存在';
        }
        return true;
    }
}